import { 
  AmbientLight,
  AnimationAction,
  AnimationMixer,
  Clock,
  DoubleSide,
  Mesh,
  MeshStandardMaterial,
  PerspectiveCamera,
  PlaneGeometry,
  RGBEEncoding,
  Scene, 
  SpotLight, 
  WebGLRenderer
} from "three";
import stats from '../../utils/stats';
// 调用
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls';
import { GUI } from 'dat.gui';
import { RGBELoader } from "three/examples/jsm/loaders/RGBELoader";

let clock = new Clock();
let scene = new Scene();
let renderer = setRenderer();
let camera = setCamera();
let mixer: AnimationMixer;

setLight();
init();

const trackballControls = new OrbitControls(camera, renderer.domElement);
// 调整场景位置
trackballControls.target.set(0, 3, 0);

render();

function render() {
  stats.update();
  trackballControls.update();

  if (mixer) {
    mixer.update(clock.getDelta())
  }

  requestAnimationFrame(render);
  renderer.render(scene, camera);
}

function setRenderer() {
  const renderer = new WebGLRenderer({ antialias: true });
  renderer.setPixelRatio(window.devicePixelRatio);
  renderer.setSize(window.innerWidth, window.innerHeight);
  // 支持阴影生成 
  renderer.shadowMap.enabled = true;
  document.body.appendChild(renderer.domElement);

  return renderer
}

function setCamera(): PerspectiveCamera {
  let camera = new PerspectiveCamera(45, window.innerWidth/window.innerHeight, 0.1, 1000);
  camera.position.set(0, 20, 40);
  scene.add( camera );

  return camera;
}

// 灯光设置
function setLight() {
  scene.add(new AmbientLight(0x444444));
  scene.add(new AmbientLight(0x343434));
  
  let spotLight = new SpotLight(0xffffff);
  spotLight.position.set(-10, 30, 40);
  spotLight.shadow.mapSize.width = 2048;
  spotLight.shadow.mapSize.height = 2048;
  spotLight.shadow.camera.fov = 15;
  spotLight.decay =2;
  spotLight.penumbra = 0.05;
  spotLight.castShadow = true;

  scene.add(spotLight);
  // scene.add(new SpotLightHelper(spotLight));
}

/** 功能区 */
function init() {
  let rgbeTextureLoader = new RGBELoader();
  rgbeTextureLoader.load(
    '/textures/hdr/dani_cathedral_oBBC.hdr',
    (texture) => {
      // texture.encoding = RGBEEncoding;
      // https://threejs.org/docs/#api/zh/textures/Texture.flipY
      // texture.flipY = true;

      let kont = new PlaneGeometry(20, 20);
      let mate = new MeshStandardMaterial({
        metalness: 0.2,
        roughness: 0.5,
        map: texture,
        side: DoubleSide,
      });
      let mesh = new Mesh(kont, mate);
      scene.add(mesh);
    }
  );

}

function addGUI(action: AnimationAction): void {
  const gui = new GUI();
}
